package com.yanqu.road.dao.impl.nenmonopolymarket;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.systemgroup.newmonopolymarket.config.NewMonopolyMarketResConfigData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NewMonopolyMarketResConfigDaoImpl extends TempDao {

    private static String getMonopolyMarketResConfigDataSql = "select * from `t_p_new_monopoly_market_res` ORDER BY `type`,`level` ASC";
    public List<NewMonopolyMarketResConfigData> getMonopolyMarketResConfigDataList() {
        List<NewMonopolyMarketResConfigData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getMonopolyMarketResConfigDataSql);
            rs = call.executeQuery();
            while (rs.next()) {
                NewMonopolyMarketResConfigData monopolyMarketResConfigData = new NewMonopolyMarketResConfigData();
                monopolyMarketResConfigData.setId(rs.getInt("id"));
                monopolyMarketResConfigData.setLevel(rs.getInt("level"));
                monopolyMarketResConfigData.setType(rs.getInt("type"));
                monopolyMarketResConfigData.setReward(rs.getString("reward"));
                monopolyMarketResConfigData.setRewardAdd(rs.getString("rewardAdd"));
                monopolyMarketResConfigData.setLimit(rs.getInt("limit"));
                monopolyMarketResConfigData.setCost(rs.getInt("cost"));
                monopolyMarketResConfigData.setIsad(rs.getInt("isad"));
                monopolyMarketResConfigData.setMonster(rs.getString("monster"));
                monopolyMarketResConfigData.setSeize(rs.getInt("seize"));
                monopolyMarketResConfigData.setPic(rs.getString("pic"));
                monopolyMarketResConfigData.setReturncost(rs.getInt("returncost"));
                monopolyMarketResConfigData.setNpcReturncost(rs.getInt("npcReturncost"));
                monopolyMarketResConfigData.setNpcList(rs.getString("npcList"));

                monopolyMarketResConfigData.setBelongBase(rs.getInt("belongBase"));
                String attackReward = rs.getString("attackReward");
                if(!StringUtils.isNullOrEmpty(attackReward)){
                    monopolyMarketResConfigData.setAttackReward(attackReward);
                }

                // 解析
                if(monopolyMarketResConfigData.getType() == eMonopolyMarketResType.OFFICE_NEW.getValue()){
                    // 榷署
                    String[] rewardStr = monopolyMarketResConfigData.getReward().split("\\|");
                    monopolyMarketResConfigData.rewardValue1 = Integer.valueOf(rewardStr[0]);
                    monopolyMarketResConfigData.rewardValue2 = Integer.valueOf(rewardStr[1]);

                    String[] rewardAddStr = monopolyMarketResConfigData.getRewardAdd().split("\\|");
                    monopolyMarketResConfigData.rewardAdd1 = Integer.valueOf(rewardAddStr[0]);
                    monopolyMarketResConfigData.rewardAdd2 = Integer.valueOf(rewardAddStr[1]);

                }else if(monopolyMarketResConfigData.getType() == eMonopolyMarketResType.RES_KING_NEW.getValue() ||
                        monopolyMarketResConfigData.getType() == eMonopolyMarketResType.RES_QUEEN_NEW.getValue()){
                    // 王帐 王宫
                    monopolyMarketResConfigData.rewardValue1 = Integer.valueOf(monopolyMarketResConfigData.getReward());
                    monopolyMarketResConfigData.rewardAdd1 = Integer.valueOf(monopolyMarketResConfigData.getRewardAdd());
                }
                list.add(monopolyMarketResConfigData);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
